// @dart=2.9
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Github API Demo',
        theme: ThemeData(
          primarySwatch: Colors.grey,
        ),
        home: const MyBody());
  }
}

class MyBody extends StatefulWidget {
  const MyBody({Key key}) : super(key: key);

  @override
  State<MyBody> createState() => _MyBodyState();
}

class _MyBodyState extends State<MyBody> {
  final _events = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Github API Demo"),
      ),
      body: Scrollbar(
        child: RefreshIndicator(
          onRefresh: () async {
            await _refresh();
          },
          child: ListView(
            children: _events.map<Widget>((event) {
              return Dismissible(
                key: ValueKey(event.id),
                child: ListTile(
                  leading: Image.network(event.avatarUrl),
                  title: Text(event.userName),
                  subtitle: Text(event.repoName),
                ),
                onDismissed: (_) {
                  setState(() {
                    _events.removeWhere((element) => element.id == event.id);
                  });
                },
                confirmDismiss: (_) async {
                  return await showDialog(
                      context: context,
                      barrierDismissible: true,
                      builder: (BuildContext context) {
                        return AlertDialog(
                          title: const Text("Are you sure?"),
                          content: const Text("Do you want to delete this item."),
                          actions: <Widget>[
                            TextButton(
                                onPressed: () => Navigator.pop(context, false),
                                child: const Text("Cancel")),
                            TextButton(
                                onPressed: () => Navigator.pop(context, true),
                                child: const Text(
                                  "Delete",
                                  style: TextStyle(color: Colors.red),
                                )),
                          ],
                        );
                      });
                },
              );
            }).toList(),
          ),
        ),
      ),
    );
  }

  _refresh() async {
    final res = await http.get("https://api.github.com/events");
    if (res.statusCode == 200) {
      final json = convert.jsonDecode(res.body);
      setState(() {
        _events.clear();
        _events.addAll(json.map((e) => GitEvent(e)));
      });
    }
  }
}

class GitEvent {
  String id;
  String userName;
  String avatarUrl;
  String repoName;

  GitEvent(json) {
    id = json["id"];
    userName = json["actor"]["login"];
    avatarUrl = json["actor"]["avatar_url"];
    repoName = json["repo"]["name"];
  }

  @override
  String toString() {
    return 'GitEvent{id: $id, userName: $userName, repoName: $repoName}';
  }
}
